package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springblade.common.utils.XxlJobUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;
import org.springblade.modules.admin.entity.MaintainScoreMsg;
import org.springblade.modules.admin.service.IMaintainScoreMsgService;
import org.springblade.core.boot.ctrl.BladeController;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 评分消息定时器设置 控制器
 *
 * @author ysq
 * @since 2025-06-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/store/msg")
@Api(value = "评分消息定时器设置", tags = "评分消息定时器设置接口")
public class MaintainScoreMsgController extends BladeController {

	private final IMaintainScoreMsgService maintainStoreMsgService;


	private final Environment environment;


	@GetMapping("test")
	public void test(Integer type) {
		if (type == 1) {
			maintainStoreMsgService.sendByStoreNoPass();
		} else {
			maintainStoreMsgService.abarbeitungPlanTimeOut();
		}
	}


	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入maintainStoreMsg")
	public R<MaintainScoreMsg> detail(MaintainScoreMsg maintainStoreMsg) {
		MaintainScoreMsg detail = maintainStoreMsgService.getOne(Wrappers.<MaintainScoreMsg>lambdaQuery().eq(MaintainScoreMsg::getTenantId, maintainStoreMsg.getTenantId()).eq(MaintainScoreMsg::getStatus, maintainStoreMsg.getStatus()));
		return R.data(detail);
	}


	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入maintainStoreMsg")
	public R submit(@Valid @RequestBody MaintainScoreMsg maintainStoreMsg) {
		maintainStoreMsg.setCron(convertToCron(maintainStoreMsg.getDayTime()));
		String curEnvironment = environment.getActiveProfiles()[0];
		if (curEnvironment.equals("dev")) {
			if (maintainStoreMsg.getStatus() == 1) {
				boolean result = XxlJobUtil.update(195, 6, "保养不合规消息提醒", "张冲冲", "CRON", maintainStoreMsg.getCron(), "FIRST", "DO_NOTHING", "SERIAL_EXECUTION");
			}
			if (maintainStoreMsg.getStatus() == 2) {
				boolean result = XxlJobUtil.update(194, 6, "整改工单逾期消息提醒", "张冲冲", "CRON", maintainStoreMsg.getCron(), "FIRST", "DO_NOTHING", "SERIAL_EXECUTION");
			}
		}
		if (curEnvironment.equals("test")) {
			if (maintainStoreMsg.getStatus() == 1) {
				boolean result = XxlJobUtil.update(202, 2, "保养不合规消息提醒", "张冲冲", "CRON", maintainStoreMsg.getCron(), "FIRST", "DO_NOTHING", "SERIAL_EXECUTION");
			}
			if (maintainStoreMsg.getStatus() == 2) {
				boolean result = XxlJobUtil.update(201, 2, "整改工单逾期消息提醒", "张冲冲", "CRON", maintainStoreMsg.getCron(), "FIRST", "DO_NOTHING", "SERIAL_EXECUTION");
			}
		}
		if (curEnvironment.equals("prod")) {
			if (maintainStoreMsg.getStatus() == 1) {
				boolean result = XxlJobUtil.update(198, 5, "保养不合规消息提醒", "张冲冲", "CRON", maintainStoreMsg.getCron(), "FIRST", "DO_NOTHING", "SERIAL_EXECUTION");
			}
			if (maintainStoreMsg.getStatus() == 2) {
				boolean result = XxlJobUtil.update(197, 5, "整改工单逾期消息提醒", "张冲冲", "CRON", maintainStoreMsg.getCron(), "FIRST", "DO_NOTHING", "SERIAL_EXECUTION");
			}
		}
		return R.status(maintainStoreMsgService.saveOrUpdate(maintainStoreMsg));
	}


	private String convertToCron(String userInput) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
		LocalTime time = LocalTime.parse(userInput, formatter);
		return String.format("0 %d %d * * ?", time.getMinute(), time.getHour());
	}

}
